<?php

/**
 * This is the model class for table "kodo_kodostudios_sourcemessage".
 */
require_once(dirname(__FILE__).'/base/SourceMessageBase.php');
class SourceMessage extends SourceMessageBase
{
    const FRONTEND_GROUP = 'Frontend';
    const BACKEND_GROUP = 'Backend';
    
    public $group;
    public $module;
    public $lang;
    public $records_list;
    
    public function getGroupsList()
    {
        return array(
            self::BACKEND_GROUP => Yii::t('Backend.Cms.LanguageForm','Back end'),
            self::FRONTEND_GROUP => Yii::t('Backend.Cms.LanguageForm','Front end'),
        );
    }
    
    public function getModulesList()
    {
        $modulesList = array_keys(Yii::app()->modules);
        $modules = array();
        foreach($modulesList as $item)
        {
            if (!in_array($item,array('gii','SiteManagement')))
                $modules[$item] = $item;    
        }
        return $modules;
    }
    
    public function getLanguagesList()
    {
        $standardLocales = LocaleManager::getStandardLocales();
        $models = Language::model()->findAll('status = true');
        $langs = array();
        foreach($models as $model)
        {
            $locale_id = $model->locale_id;        
            $localeIdParts = explode('_',$locale_id);
            $localeIdParts[1] = strtoupper($localeIdParts[1]);
            $new_locale_id = implode('_',$localeIdParts);
            if (isset($standardLocales[$locale_id]))
                $locale_name = $standardLocales[$locale_id];
            else
                $locale_name = $new_locale_id;   
            $langs[$new_locale_id] = $locale_name;
        }
        return $langs;
    }
    
    public function getCurrentLanguage()
    {
        $localeIdParts = explode('_',Yii::app()->locale->id);
        $localeIdParts[1] = strtoupper($localeIdParts[1]);    
        return implode('_',$localeIdParts);
    }
    
    public function getTranslatedMessagesWithTypes($lang, $group, $module)
    {
        $source_messages_models = SourceMessage::model()->findAll("category LIKE '{$group}.{$module}.%'");
        $source_messages = array();
        foreach($source_messages_models as $item)
        {
            $type = str_replace("{$group}.{$module}.",'',$item->category);        
            if (!isset($source_messages[$type]))
                $source_messages[$type] = array();
            $translated_message = TranslatedMessage::model()->find('id=:id AND language=:lang',array(':id'=>$item->id,':lang'=>$lang));
            if (!is_null($translated_message))
                $source_messages[$type][$item->id] = array('source'=>$item->message,'translation'=>$translated_message->translation);
            else
                $source_messages[$type][$item->id] = array('source'=>$item->message,'translation'=>$item->message);
        }
        return $source_messages;
    }
    
    public function saveData()
    {
        if (is_array($this->records_list) && count($this->records_list) > 0)
        {
            foreach($this->records_list as $index_group => $list_groups)
            {
                foreach($list_groups as $index_module => $list_modules)
                {
                    foreach($list_modules as $index_type => $list_type)
                    {
                        foreach($list_type as $source_id => $targetMsg)
                        {
                            $criteria = new CDbCriteria();
                            $criteria->join = 'INNER JOIN "public"."'.SITE_TABLE_PREFIX.'sourcemessage" "s" ON s.id = t.id';
                            $criteria->compare('t.id',$source_id);
                            //$criteria->compare('s.category',$index_group.'.'.$index_module.'.'.$index_type);
                            $criteria->compare('t.language',$this->lang);
                            $model = TranslatedMessage::model()->find($criteria);
                            if (!is_null($model))
                            {
                                $model->translation = $targetMsg;
                                $model->update(array('translation'));
                            }
                            else
                            {
                                $model = new TranslatedMessage();
                                $model->id = $source_id;
                                $model->language = $this->lang;
                                $model->translation = $targetMsg;
                                $model->save();
                            }
                        }
                    }
                }    
            }
        }
    }
}